<?php
function source_exists($sid) {
  $sql = sprintf('select * from source where id="%s"', $sid);
  $ret = mysql_query($sql);
  if (!$ret) {
    die("in source_exists, sql error: " . mysql_error());
  }
  return mysql_num_rows($ret) > 0;
}

function remove_source($sid) {
  $sid = mysql_real_escape_string($sid);
  $sql = sprintf('delete from source where id = "%s"', $sid);
  if (!mysql_query($sql)) {
    return array('success' => 'no', 'error' => mysql_error(), 'sql' => $sql);
  }

  $sql = sprintf('delete from source_item where source_id = "%s"', $sid);
  if (!mysql_query($sql)) {
    return array('success' => 'no', 'error' => mysql_error(), 'sql' => $sql);
  }
  return array('success' => 'yes');
}

function parse_source_info($data) {
  // get base_url
  $base_url = mysql_real_escape_string($data['base_url']);

  // get source_id
  if (isset($data['source_id']) && strlen(mysql_real_escape_string($data['source_id'])) > 0) {
    $sid = mysql_real_escape_string($data['source_id']);
  } else {
    $sid = '';
  }

  // get need_query
  if(isset($data['need_kw']) && $data['need_kw'] == "yes"){
    $need_query = 1;
  } else {
    $need_query = 0;
  }

  // get encoding
  if (isset($data['encoding']) && strlen(mysql_real_escape_string($data['encoding'])) > 0) {
    $encoding = mysql_real_escape_string($data['encoding']);
  } else {
    $encoding = get_encoding($base_url);
  }

  // get tags
  $tag_sum = "";
  if (isset($data['tags'])) {
    foreach ($data['tags'] as $tag) {
      if (strlen($tag) > 0) {
        $tag_sum = $tag_sum . "#$tag";
      }
    }
    $tag_sum = substr($tag_sum, 1);
  }

  // get is_prime
  $is_prime = isset($data['is_prime']) ? 1 : 0;

  // get domain
  $domain = $data['domain'];

  return array($sid, $base_url, $need_query, $encoding, $tag_sum, $is_prime, $domain);
}

function add_source_item($data) {
  $title = mysql_real_escape_string($data['title_xpath']);
  $content = mysql_real_escape_string($data['content_xpath']);
  $image = mysql_real_escape_string($data['image_xpath']);
  $href = mysql_real_escape_string($data['href_xpath']);
  $sid = mysql_real_escape_string($data['source_id']);


  $sql = sprintf('insert into source_item (source_id,title_xpath,content_xpath,image_xpath,href_xpath) values("%s", "%s", "%s", "%s", "%s")',
                 $sid, $title, $content, $image, $href);

  if (strpos($sql, '^') && !strpos($sql, '^1') && !strpos($sql, '^2')) {
    $sql = str_replace('^', '^1', $sql);
  }

  $wild_low1 = $data['wild_low1'];
  $wild_high1 = $data['wild_high1'];
  $wild_delta1 = $data['wild_delta1'];

  $wild_low2 = $data['wild_low2'];
  $wild_high2 = $data['wild_high2'];
  $wild_delta2 = $data['wild_delta2'];

  $para_lists = array();
  $para_lists[] = range($wild_low1, $wild_high1, $wild_delta1);
  $para_lists[] = range($wild_low2, $wild_high2, $wild_delta2);

  $sql_list = expand_wildcard($sql, $para_lists[0], $para_lists[1]);

  $ret = execute_all_sql_or_die($sql_list);
  if ($ret != -1) {
    echo $ret . ' items added to DB';
  }
}


function add_source($data) {
  list($sid, $base_url, $need_query, $encoding, $tag_sum, $is_prime, $domain) =
    parse_source_info($data);

  if (!$base_url) {
    echo "你提交的base_url为空, 这是什么源??? 反正我没法给你提交，你按后退吧";
    return -1;
  }

  if ($sid == '') {
    $sid = sprintf("%s.%d", parse_url($base_url, PHP_URL_HOST), time());
  }

  // Check to see if source with the same ID exists
  if (source_exists($sid)){
    echo sprintf("无法添加源: 源id(%s) 已经存在，你可以按后退了", $sid);
    return -1;  
  }

  // insert source
  $sql = sprintf('insert into source (id,base_url,encoding,need_query, tags, is_prime, domain) 
values("%s", "%s", "%s", "%d", "%s", %d, "%s")',
                 $sid, $base_url, $encoding, $need_query, $tag_sum, $is_prime, $domain);
  $ret = mysql_query($sql);

  if (!$ret || mysql_affected_rows() == 0) {
    echo sprintf("添加源失败了,sql:(%s), MYSQL error: (%s)", $sql, mysql_error());
    return -1;
  }
}

function edit_source($data) {
  /* var_dump($data); */
  /* exit(0); */
  list($sid, $base_url, $need_query, $encoding, $tag_sum, $is_prime, $domain) =
    parse_source_info($data);

  if (!source_exists($sid)) {
    echo sprintf("无法编辑源: 源id(%s)在数据库中不存在，你可以按后退了", $sid);
    return -1;
  }
      
  $sql = sprintf('update source set 
base_url = "%s", encoding = "%s", need_query = %d, tags = "%s", is_prime = %d, domain = "%s"
where id = "%s"',
                 $base_url, $encoding, $need_query, $tag_sum, $is_prime, $domain, $sid);

  $ret = mysql_query($sql);
  if (!$ret) {
    echo sprintf('更新源基本信息失败，SQL(%s)，错误:(%s)', $sql, mysql_error());
    return -1;
  } else {
    echo mysql_affected_rows() . ' rows affected';
  }
}

?>